package com.hyx.java.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyx.java.base.BaseResult;
import com.hyx.java.base.PageBean;
import com.hyx.java.constant.HyxConstant;
import com.hyx.java.dto.HyxUserAddReq;
import com.hyx.java.dto.HyxUserSearchReq;
import com.hyx.java.dto.HyxUserUpdateReq;
import com.hyx.java.entity.HyxUser;
import com.hyx.java.mapper.HyxUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class HyxUserService {

    @Autowired
    private HyxUserMapper userMapper;

    public PageBean userPage(HyxUserSearchReq search) {
        PageBean pageBean = new PageBean(search.getCurPage(), search.getPageSize());
        LambdaQueryWrapper<HyxUser> wrapper = new QueryWrapper<HyxUser>().lambda()
            .eq(StringUtils.isNotBlank(search.getUserName()), HyxUser::getUserName, search.getUserName())
            .eq(StringUtils.isNotBlank(search.getRealName()), HyxUser::getRealName, search.getRealName());
        wrapper.orderByDesc(HyxUser::getId);
        IPage<HyxUser> userPage = userMapper.selectPage(
            new Page<>(pageBean.getCurPage(), pageBean.getPageSize()),wrapper);
        pageBean.setRecords(userPage.getRecords());
        pageBean.setPages(userPage.getPages());
        pageBean.setTotal(userPage.getTotal());
        return pageBean;
    }

    public BaseResult add(HyxUserAddReq user) {
        HyxUser one = userMapper.selectOne(new QueryWrapper<HyxUser>().lambda().eq(HyxUser::getUserName, user.getUserName()));
        if (one != null) {
            return BaseResult.fail("013", "已存在相同账号：" + user.getUserName());
        }
        userMapper.insert(HyxUser.builder().userName(user.getUserName())
            .password(DigestUtil.md5Hex(StrUtil.isEmpty(user.getPassword()) ? HyxConstant.DEFAULT_PASSWORD : user.getPassword()))
            .phone(user.getPhone()).realName(user.getRealName()).build());
        return BaseResult.success();
    }

    public BaseResult update(HyxUserUpdateReq user) {
        HyxUser one = userMapper.selectOne(new QueryWrapper<HyxUser>().lambda()
            .eq(HyxUser::getId, user.getId())
            .eq(HyxUser::getUserName, user.getUserName()));
        if (one == null) {
            return BaseResult.fail("013", "未找到用户信息");
        }
        one.setPhone(user.getPhone());
        one.setRealName(user.getRealName());
        userMapper.updateById(one);
        return BaseResult.success();
    }

    public BaseResult delete(Long userId) {
        userMapper.deleteById(userId);
        return BaseResult.success();
    }

    public BaseResult resetPassword(Long userId) {
        userMapper.updateById(HyxUser.builder().id(userId)
            .password(DigestUtil.md5Hex(HyxConstant.DEFAULT_PASSWORD)).build());
        return BaseResult.success();
    }

}
